package com.module.user.utils

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.common.base.utils.AppUtils

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/29
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
object UserPermissionUtils {
    /**
     * 访问相机、文件的权限
     */
    val REQ_PERMISSION_CODE = 0x100

    fun checkPermission(activity: AppCompatActivity?): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity != null) {
            val permissions = java.util.ArrayList<String>()
            if (PackageManager.PERMISSION_GRANTED != activity.let {
                    ActivityCompat.checkSelfPermission(
                        it,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    )
                } ||
                PackageManager.PERMISSION_GRANTED != activity.let {
                    ActivityCompat.checkSelfPermission(
                        it,
                        Manifest.permission.CAMERA
                    )
                }) {
                permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
                permissions.add(Manifest.permission.CAMERA)
            }
            if (permissions.size != 0) {
                ActivityCompat.requestPermissions(
                    activity,
                    permissions.toTypedArray(),
                    REQ_PERMISSION_CODE
                )
                return false
            }
        }

        return true
    }

    /**
     * 跳转到权限设置界面
     */
    private fun getAppDetailSettingIntent(activity: Activity) {
        val intent = Intent()
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        intent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
        intent.data = Uri.fromParts("package", AppUtils.getPackageName(), null)
        activity.startActivity(intent)
    }
}